﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using vtpsms.Classes;
using vtpsms.dao.Models;

namespace vtpsms
{
    public partial class _Default : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void ClearForm()
        {
            tbxContent.Text = string.Empty;
        }
        private void BindGrid()
        {
            var lstGroup = UnitOfWork.SmsRepository.Get().ToList();
            GrvSms.DataSource = lstGroup;
            GrvSms.DataBind();
        }

        protected void btnSaveAndSend_Click(object sender, EventArgs e)
        {
            if (fileList.HasFile)
            {
                var theStream = fileList.PostedFile.InputStream;
                var smsNumber = "";
                using (var sr = new StreamReader(theStream))
                {
                    smsNumber = sr.ReadToEnd();
                }
                var arrSuccessPhone = new List<string>();
                var arrFailedPhone = new List<string>();
                if (!string.IsNullOrEmpty(smsNumber))
                {
                    var arrSms = smsNumber.Split(new[] { ';' });
                    foreach (var phoneNumber in arrSms.Select(item => (from t in item
                                                                       where char.IsDigit(t)
                                                                       select t).ToArray()).Select(getNumbers => string.Join("", getNumbers)))
                    {
                        if (SendSms(tbxContent.Text, phoneNumber, 1, 1))
                        {
                            arrSuccessPhone.Add(phoneNumber);
                        }
                        else
                        {
                            arrSuccessPhone.Add(phoneNumber);
                        }
                    }
                }
                var membershipUser = Membership.GetUser();
                if (membershipUser != null)
                {
                    var smsItem = new SMS_TABLE
                    {
                        CreateAt = DateTime.Now,
                        SmsContent = tbxContent.Text,
                        SmsContentNoUnicode = tbxContent.Text,
                        CreateBy = membershipUser.UserName,
                        IsActive = false,
                        Status = 1,
                        NumberSuccess = string.Join(";", arrSuccessPhone),
                        NumberFailed = string.Join(";", arrFailedPhone)
                    };
                    UnitOfWork.SmsRepository.Insert(smsItem);
                }
                ClearForm();
                BindGrid();
            }
            else
            {
                lblErr.Text = "Bạn chưa chọn file danh sách";
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            ClearForm();
            pngDetail.Visible = false;
        }

        protected void BtnCreateSms_Click(object sender, EventArgs e)
        {
            pngDetail.Visible = true;
        }

        private bool SendSms(string smsContent, string smsNumber, int status, int creatBy)
        {
            //Call Service
            return true;
        }
    }
}
